package com.example.myjavafx.controller;

import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.util.CoreMap;
import javafx.concurrent.Task;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextArea;

import java.util.Properties;

public class SentimentController {
    @FXML
    private TextArea inputText;
    @FXML
    private Label resultLabel;
    @FXML
    private ProgressIndicator progressIndicator;

    private StanfordCoreNLP pipeline;

    @FXML
    private void initialize() {
        // 初始化Stanford CoreNLP
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");

        // 在后台线程初始化pipeline
        Task<Void> initTask = new Task<>() {
            @Override
            protected Void call() {
                pipeline = new StanfordCoreNLP(props);
                return null;
            }
        };

        initTask.setOnSucceeded(e -> {
            progressIndicator.setVisible(false);
            inputText.setDisable(false);
        });

        // 显示加载指示器
        progressIndicator.setVisible(true);
        inputText.setDisable(true);

        Thread initThread = new Thread(initTask);
        initThread.setDaemon(true);
        initThread.start();
    }

    @FXML
    private void analyzeSentiment() {
        String text = inputText.getText().trim();
        if (text.isEmpty()) {
            resultLabel.setText("请输入要分析的文本");
            return;
        }

        progressIndicator.setVisible(true);

        Task<String> analysisTask = new Task<>() {
            @Override
            protected String call() {
                Annotation annotation = new Annotation(text);
                pipeline.annotate(annotation);

                // 获取情感分析结果
                String sentimentResult = "";
                for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
                    String sentiment = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
                    int score = RNNCoreAnnotations.getPredictedClass(sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class));

                    // 将英文情感转换为中文
                    String chineseSentiment = switch (sentiment.toLowerCase()) {
                        case "very positive" -> "非常积极";
                        case "positive" -> "积极";
                        case "neutral" -> "中性";
                        case "negative" -> "消极";
                        case "very negative" -> "非常消极";
                        default -> sentiment;
                    };

                    sentimentResult += "情感倾向: " + chineseSentiment + "\n";
                    sentimentResult += "情感强度: " + score + "/4\n";
                }
                return sentimentResult;
            }
        };

        analysisTask.setOnSucceeded(e -> {
            resultLabel.setText(analysisTask.getValue());
            progressIndicator.setVisible(false);
        });

        analysisTask.setOnFailed(e -> {
            resultLabel.setText("分析过程出现错误，请重试");
            progressIndicator.setVisible(false);
        });

        Thread analysisThread = new Thread(analysisTask);
        analysisThread.setDaemon(true);
        analysisThread.start();
    }
}